<?php
require_once 'class/space/Room.php';
require_once 'class/item/Container.php';
require_once 'class/lib/utils.php';
require_once 'Action.php';
/**
 * Look class, perform the look command
 * @static
 * @package action
 * @author alcaitiff
 */
class Look implements Action
{
	/**
     * @static
     * @var integer
     **/
	static private $tick;
    /**
     * @static
     * @var Char
     **/
	static private $char;
	/**
    *	Usage: look [string [integer] [string [integer]]]
	*
	*	If the 3th parameter is not present or the second parameter is a number this command will try to find an element in this sequence:
	*
    *	1º a character;
    *	2º a direction;
    *	3º an item on the right hand;
    *	4º an item on the left hand;
    *	5º an item on the floor of the current room;
	*
	*	Otherwise this command will try to find a container and the item inside of it. The container may be on the right hand, on the left hand or on the floor. 
	* @static
    * @param Command $command Command
    * @param int $tick Tick
    * @return Char|Room|String This command will return the element
    */
	static public function run($command,$tick){
        /*	Parameters:
        *
        *	0: 																						look my room;
        *   1: Element name (string) 																look char|item|direction 1 on me or my room;
        *	2: Element name (string), Element Number (integer >0) 									look char|item|direction n on me or my room;
        *	2: Element name (string), Container (string)											look item 1 in container 1;
        *	3: Element name (string), Container(string), Number (integer >0)						look item 1 in container n;
        *	3: Element name (string), Number (integer >0), Container(string)						look item n in container 1;
        *	4: Element name (string), Number (integer >0), Container (string), Number (integer >0)	look item n in container n;
        *	An element can be:
        *
        *	Character -> isCharacter(string partial name,integer room id,[integer index])
        *	Item -> isItem(string partial name, string char id,[integer index])
        *	Item -> haveItem(string partial name, string item container id,[integer index])
        *	Direction -> west|east|north|south|up|down
        *
        */
		self::$tick=$tick;
		self::$char=$command->character;
        $parameters=$command->getArrangedParameters(0, 4, false);
        if($parameters===NULL){
            MessageSystem::addErrorMessage('Invalid Parameters');
            return null;
        }elseif(count($parameters)==0){
            return self::$char->getRoom();
        }else{
            if(isset($parameters[3])){
                return self::getItemOn($parameters[1], $parameters[2], $parameters[3], $parameters[4]);
            }else{
                $el=self::$char->getElement($parameters[1],$parameters[2]);
                if($el instanceof  Char){
                    $el->hideItemsContent();
                }
                return $el;
            }
        }
	}

    /**
     * @static
     * @param string $itemName Item name
     * @param int $itemIndex Item number
     * @param string $containerName Container Name
     * @param int $containerIndex Container number
     * @return Char|Room|String|Array
     */
	static private function getItemOn($itemName,$itemIndex,$containerName,$containerIndex){
		$container=self::$char->getElement($containerName,$containerIndex);
		if($containerIndex>1){
			$containerStr=$containerName.'['.$containerIndex.']';
		}else{
			$containerStr=$containerName;
		}
		if($container==null){
			return null;
		}
		if($container instanceof Item){
			if($container->container!=null){
				return self::getItemFrom($itemName,$itemIndex,$container);
			}else{
                MessageSystem::addErrorMessage($containerStr.' is not a container');
				return null;
			}
		}elseif($container instanceof Char){
            MessageSystem::addErrorMessage($containerStr.' is a character');
			return null;
		}elseif($container instanceof Room){
            MessageSystem::addErrorMessage($containerStr.' is a direction');
			return null;
		}else{
            MessageSystem::addErrorMessage($containerStr.' is not a container');
			return null;
		}
	}
    /**
     * @static
     * @param string $itemName Item name
     * @param int $itemIndex Item number
     * @param string $itemContainer Container Name
     * @return Char|Room|String|Array
     */
	static private function getItemFrom($itemName,$itemIndex,$itemContainer){
		return $itemContainer->container->getItemByName($itemName,$itemIndex);
	}
}